/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.eplatform.modules.sys.web;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.UnauthorizedException;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.web.util.WebUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.collect.Maps;
//import com.taobao.api.ApiException;
import com.eplatform.common.config.Global;
import com.eplatform.common.security.shiro.session.SessionDAO;
import com.eplatform.common.servlet.ValidateCodeServlet;
import com.eplatform.common.utils.CacheUtils;
import com.eplatform.common.utils.CookieUtils;
import com.eplatform.common.utils.ExtData;
import com.eplatform.common.utils.IdGen;
import com.eplatform.common.utils.SMSUtils;
import com.eplatform.common.utils.StringUtils;
import com.eplatform.common.web.BaseController;
import com.eplatform.modules.sys.security.FormAuthenticationFilter;
import com.eplatform.modules.sys.security.SystemAuthorizingRealm.Principal;
import com.eplatform.modules.sys.service.SMSService;
import com.eplatform.modules.sys.service.SystemService;
import com.eplatform.modules.sys.utils.LogUtils;
import com.eplatform.modules.sys.utils.UserUtils;

/**
 * 登录Controller
 * @author ThinkGem
 * @version 2013-5-31
 */
@Controller
public class SMSController extends BaseController{
	
	@Autowired
	private SessionDAO sessionDAO;
	
	@Autowired
	private SMSService smsService;
	@RequestMapping(value = "/sms/smssend")
	@ResponseBody()
	public Map smsSend(HttpServletRequest request, HttpServletResponse response) //throws ApiException
	{
		Map result=null;
		Map checkM=Maps.newHashMap();
		checkM.put("loginName", request.getParameter("pop_username"));
		checkM.put("mobile", request.getParameter("pop_mobile"));
		boolean checkMobile=smsService.checkMobile(checkM);
		if(!checkMobile){
			return ExtData.Result.Error("用户名或手机号错误 ！");
		}
		boolean check=smsService.checkStatus(request);
		if(!check){
			return ExtData.Result.Error("暂不能发送短信！");
		}
		Map params=request.getParameterMap();
		
		result=smsService.sendSMS(params,request);
		return result;
	}
	
	 @RequestMapping(value = "/sms/modifyPwd")
	 @ResponseBody()
	 public Map modifyPwd(HttpServletRequest request, HttpServletResponse response, Model model) {
		 String validateImgCode=(String)request.getSession().getAttribute(ValidateCodeServlet.VALIDATE_CODE);
		 String formImgCode=request.getParameter("validateCode");
		 boolean codeResult=StringUtils.equalsIgnoreCase(validateImgCode, formImgCode);
		 if(!codeResult){
			 return ExtData.Result.Error("图片验证码错误！");
		 }
		 String validatePhoneCode=(String)request.getSession().getAttribute(SMSUtils.SESSIONKEY);
		 String formPhoneCode=request.getParameter("pop_phonevalidatecode");
		 codeResult=StringUtils.equalsIgnoreCase(validatePhoneCode, formPhoneCode);
		 if(!codeResult){
			 return ExtData.Result.Error("短信验证码错误！");
		 }
		 String loginName= request.getParameter("pop_username");
		 String mobile=request.getParameter("pop_mobile");
		 Map checkM=Maps.newHashMap();
			checkM.put("loginName",loginName);
			checkM.put("mobile", mobile);
		 boolean checkMobile=smsService.checkMobile(checkM);
			if(!checkMobile){
				return ExtData.Result.Error("用户名或手机号错误 ！");
			}
		 String password=request.getParameter("pop_password");
		 Map params=Maps.newHashMap();
		 params.put("loginName", loginName);
		 params.put("mobile", mobile);
		 params.put("password", SystemService.entryptPassword(password));
		 smsService.modifyPwd(params);
		 LogUtils.saveLog(request, "user:"+loginName+",通过短信验证修改密码！");
		 return ExtData.Result.Success;
	 }
	 
}
